package com.apes.fn.business.fixedLoss.modle;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Category;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import com.apes.scm.masterdata.product.model.ServeType;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * @ClassName : LossQuotation
 * @Description : 定损报价单确认方案
 * @Author : WSJ
 * @Date: 2020-03-05 16:14
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = LossQuotationPlan.CONST_TABLE_NAME)
public class LossQuotationPlan extends Model {
    public final static String CONST_TABLE_NAME = "ser_loss_quotation_plan";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "定损报价单明细", padding = 4)
    @Comment("ID")
    private String id;

    @Column(name = "sort", length = 250)
    @Comment("排序字段")
    private String sort;

    @ManyToOne(optional = false)
    @JoinColumn(name = "loss_Quotation_id", referencedColumnName = "id")
    @Comment("定损报价单号")
    private LossQuotation lossQuotation;

    @Column(name = "higher_level_code", length = 24)
    @Comment("上级商品编码")
    private String higherLevelCode;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位编码")
    private ProductUom uom;

    @Column(name = "sale_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("销售数量")
    private double saleQty = 0;


    // @Column(name = "primary_price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    // @Comment("最低定损价")
    // private double primaryPriceUnit = 0;

    @Column(name = "fixed_Loss_Price", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("定损单价")
    private double fixedLossPrice = 0;

    @Column(name = "min_Fixed_Loss_Price", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("最低定损单价")
    private double minFixedLossPrice = 0;

    @Column(name = "fixed_Loss_Amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("定损金额")
    private double fixedLossAmount = 0;

    @Column(name = "min_Fixed_Loss_Amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("最低定损金额")
    private double minFixedLossAmount = 0;

    @Column(name = "price_Difference", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("差价合计")
    private double priceDifference = 0;

    @Column(name = "sales_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售金额")
    private double salesAmount = 0;

    @Column(name = "floor_price", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("最低限价")
    private double floorPrice = 0;

    @Column(name = "clerk_price_limmit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("店员限价")
    private double clerkPriceLimit = 0;

    @Column(name = "shop_owner_price_limit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("店长限价")
    private double shopOwnerPriceLimit = 0;

    @Column(name = "cso_price_limit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售总监限价")
    private double csoPriceLimit = 0;

    @Column(name = "base_Price", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售基准价")
    private double basePrice = 0;

    @Column(name = "discount_rate", nullable = false, columnDefinition = ApesConst.FIELD_ROUND_16_8)
    @Comment("折扣率")
    private double discountRate = 0;

    @Column(name = "dividend_store", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("接车门店分成率")
    private double dividendStore = 0;


    @Column(name = "dividend_store_old", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("接车门店分成率(旧分成率)")
    private double dividendStoreOld = 0;

    @Column(name = "platform_price_cost_in_tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("平台成本单价")
    private double platformPriceCosInTax = 0;

    @Column(name = "price_cost_unit_in_tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("含税成本单价")
    private double priceCostUnitInTax = 0;

    @Column(name = "price_cost_unit_ex_tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("不含税成本单价")
    private double priceCostUnitExTax = 0;

    @Column(name = "pay_Yourself", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("自费")
    private boolean payYourself = false;

    @Column(name = "product_rule", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("带安标记")
    private boolean productRule = false;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id", nullable = false)
    private Location location;  //仓库库区

    @Column(name = "remarks", length = 256)
    @Comment("备注")
    private String remarks;

    @Column(name = "customer_Remarks", length = 256)
    @Comment("客服备注")
    private String customerRemarks;

    @Comment("服务方案单号")
    @Column(name = "service_Plan", length = 32)
    private String servicePlan;

    @Column(name = "profit", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("毛利率")
    private double profit = 0;

    @Transient
    @Comment("品类")
    private Category category ;

    public Category getCategory(){
        if(this.fitting != null) return  this.fitting;
        if(this.product != null) return this.product.getCategory();
        return null;
    }
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fitting", referencedColumnName = "id")
    @Comment("配件")
    private Category fitting;


    @Column(name = "carrier_id")
    @Comment("承运商编码")
    private String carrierId;

    @Column(name = "carrier_name")
    @Comment("承运商名称")
    private String carrierName;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="serve_type_id", referencedColumnName = "id")
    @Comment("服务类型")
    private ServeType serveType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "platform_id", referencedColumnName = "id")
    @Comment("平台")
    private PartyRole platform;

    @Column(name = "expected_arrival_time", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("预计到货时长")
    private double expectedArrivalTime = 0;

    @Column(name = "supply_Chain_Management", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("供应链管理费用")
    private double supplyChainManagement = 0;

    @Column(name = "refer_Tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("参考成本")
    private double referTax = 0;

    @Column(name = "estimated_Commission", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("预估提成单价")
    private double estimatedCommission = 0;


    @Column(name = "done", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("转服务标记")
    private boolean done = false;


    @Transient
    private double expectedArrivalTimeTemp;
    public double getExpectedArrivalTimeTemp(){
        if("2".equals(this.inquiryGooodsState)){
            return Arith.round(this.expectedArrivalTime / 24,3);
        }
        return 0;
    }

    @Transient
    private String lossQuotationId;

    public String getLossQuotationId() {
       if (this.lossQuotation !=null) {
          return   this.lossQuotation.getId();
       }else {
           return null;
       }
    }
    //计算单个商品得毛利
    public void computeProfit() {
        if (this.fixedLossAmount >0)
            this.profit = Arith.div( this.fixedLossAmount - this.minFixedLossAmount,this.fixedLossAmount,2);
        this.profit = Arith.roundUp(this.profit * 100, 2);
    }

    @Column(name = "old_inquiry_number", length = 32)
    @Comment("老框架询价单号")
    private String oldInquiryNumber;

    @Column(name = "unique_value", length = 4000)
    @Comment("唯一值")
    private String uniqueValue;

    @Column(name = "inquiry_gooods_state", length = 32)
    @Comment("货源状态(0无货|1现货|2需订货)")
    private String inquiryGooodsState;

    @Column(name = "replace_With_Repair",columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("以修代换标识")
    private boolean replaceWithRepair = false;

    @Column(name = "price_forward")
    @Comment("渠道")
    private String priceForward;//1==价格前置标记，2==新平台询价 ， 3==询价执行表 4==三头六臂

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @Comment("供应商")
    private PartyRole supplier; //供应商

    @Comment("报价结果Id")
    @JoinColumn(name = "quote_result_id")
    private String quoteResultId;
}
